package com.rxyl.love.petdoctor.util;

import android.util.Log;
import android.widget.Toast;

import com.rxyl.love.petdoctor.entity.CityEntity;
import com.rxyl.love.petdoctor.entity.DistrictEntity;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by dd on 2016/1/12 0012.
 */
public class CityJSON {

    static String[] labels = {"A", "B", "C", "D", "E", "F", "G",
            "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T",
            "U", "V", "W", "X", "Y", "Z"};

    public static List<CityEntity> getCitysByJSON(String json) {
        if (json != null) {
            List<CityEntity> cityEntities = new ArrayList<>();
            try {
                JSONObject jsonObject = new JSONObject(json);
                if (jsonObject.getInt("status") == 10000) {
                    JSONArray array = jsonObject.getJSONArray("data");
                    for (int i = 0; i < array.length(); i++) {
                        CityEntity cityEntity = new CityEntity();
                        JSONObject object = array.getJSONObject(i);
                        cityEntity.setCity_id(object.getInt("city_id"));
                        cityEntity.setCity_name(object.getString("city_name"));
                        cityEntity.setCity_letter(object.getString("city_letter"));
                        cityEntities.add(cityEntity);
                    }
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
            for (int i = 0; i < labels.length; i++) {
                CityEntity cityEntity = new CityEntity(labels[i], 0);
                for (CityEntity entity : cityEntities) {
                    if (labels[i].toLowerCase().equals(entity.getCity_letter())) {
                        cityEntities.add(cityEntities.indexOf(entity), cityEntity);
                        break;
                    }
                }

            }
            return cityEntities;
        }
        return null;
    }

    public static List<DistrictEntity> getDistrictByJSON(String json, int cityid) {
        if (json != null) {
            List<DistrictEntity> districtEntities = new ArrayList<>();
            try {
                JSONObject jsonObject = new JSONObject(json);
                if (jsonObject.getInt("status") == 10000) {
                    JSONArray array = jsonObject.getJSONArray("data");
                    for (int i = 0; i < array.length(); i++) {
                        JSONObject object = array.getJSONObject(i);
                        if (cityid == object.getInt("city_id")) {
                            JSONArray array1 = object.getJSONArray("districts");
                            if (array1 != null) {
                                for (int j = 0; j < array1.length(); j++) {
                                    JSONObject object1 = array1.getJSONObject(j);
                                    DistrictEntity districtEntity = new DistrictEntity();
                                    districtEntity.setDistrict_id(object1.getInt("district_id"));
                                    districtEntity.setDistrict_name(object1.getString("district_name"));
                                    districtEntities.add(districtEntity);
                                }
                            }
                        }
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            DistrictEntity districtEntity = new DistrictEntity();
            districtEntity.setDistrict_name("全部");
            districtEntities.add(0, districtEntity);
            return districtEntities;
        }
        return null;
    }
}
